package com.example.flashsales.service.impl;

import com.example.flashsales.entity.pojo.AdminCostRecord;
import com.example.flashsales.entity.vo.AdminCostRecordVo;
import com.example.flashsales.mappers.AdminCostRecordMapper;
import com.example.flashsales.service.AdminCostRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdminCostRecordServiceImpl implements AdminCostRecordService {
    @Autowired
    AdminCostRecordMapper adminCostRecordMapper;

    /**
     * 查看流水纪录 按时间（按天）
     * @param qTime 前一天
     * @param hTime 后一天
     * @param page 页数
     * @param num 一页的条数
     * @return 返回流水视图对象集合
     */
    @Override
    public List<AdminCostRecordVo> viewCostRecordByTimeDay(Date qTime,Date hTime, int page, int num) {
        if (page <= 0){
            return null;
        }
        page = (page-1)*num;
        List<AdminCostRecordVo> adminCostRecordVos = new ArrayList<>();
        for (AdminCostRecord adminCostRecord : adminCostRecordMapper.viewCostRecordByTimeDay(qTime, page, num, hTime)) {
            AdminCostRecordVo Vo = new AdminCostRecordVo();
            BeanUtils.copyProperties(adminCostRecord,Vo);
            if (adminCostRecord.getAType() == 0){
                Vo.setAType("进货");
            }else if(adminCostRecord.getAType() == 1){
                Vo.setAType("商家提现");
            }else if (adminCostRecord.getAType() == 2){
                Vo.setAType("用户订单");
            }else if (adminCostRecord.getAType() == 3){
                Vo.setAType("用户退款");
            } else {
                Vo.setAType("未知");
            }
            adminCostRecordVos.add(Vo);
        }
        return adminCostRecordVos;
    }





    /**
     * 增加一条纪录
     * @param type 类型
     * @param money 金钱
     * @return
     */
    @Override
    public String addCostRecord(int type, double money) {
        if (type == 0 || type == 1){
            money = money * -1;
        }
        if (adminCostRecordMapper.addCostRecord(type, money) > 0) {
            return "添加成功";
        }
        return "添加失败";
    }


    /**
     * 看所有的流水
     * @param page 页数
     * @param num 一页的条数
     * @return 返回流水视图对象集合
     */
    @Override
    public List<AdminCostRecordVo> viewAllCostRecord(int page,int num) {
        if (page <= 0){
            return null;
        }
        page = (page-1)*num;
        final List<AdminCostRecord> adminCostRecords = adminCostRecordMapper.viewAllCostRecord(page, num);
        List<AdminCostRecordVo> adminCostRecordVos = new ArrayList<>();
        for (AdminCostRecord adminCostRecord : adminCostRecords) {
            AdminCostRecordVo adminCostRecordVo = new AdminCostRecordVo();
            BeanUtils.copyProperties(adminCostRecord,adminCostRecordVo);
            if (adminCostRecord.getAType() == 0){
                adminCostRecordVo.setAType("进货");
            }else if(adminCostRecord.getAType() == 1){
                adminCostRecordVo.setAType("商家提现");
            }else if (adminCostRecord.getAType() == 2){
                adminCostRecordVo.setAType("用户订单");
            }else {
                adminCostRecordVo.setAType("未知");
            }
            adminCostRecordVos.add(adminCostRecordVo);
        }
        return adminCostRecordVos;
    }


    /**
     * 查看流水纪录
     * @param page
     * @param num
     * @param qDate
     * @param hDate
     * @param state
     * @return
     */
    @Override
    public List<AdminCostRecordVo> viewCost(Integer page, Integer num, Date qDate, Date hDate, Integer state) {
        PageHelper.startPage(page,num);
        return adminCostRecordMapper.viewCost(qDate, hDate, state);
    }

    /**
     * 按类型统计钱
     * @param state
     * @return
     */
    @Override
    public Double viewBillByState(Integer state,Date qDate,Date hDate) {

        return adminCostRecordMapper.viewBill(state,qDate,hDate);
    }
}
